package com.bss.controller;

import com.bss.pojo.Category;
import com.bss.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author 袁飞
 * @company 中原工学院
 * @create 2020-03-14-19:49
 */
@RestController
@RequestMapping("/bss/category")
public class CategoryController {
    @Autowired
    CategoryService CategoryService;
    @RequestMapping("find")
    public Category getCategory(@RequestParam("id") Integer id){
        return CategoryService.findCategoryById(id);
    }
    @RequestMapping("addOrUpd")
    public void addOrUpd(Category Category){
        if ((Category.getCategoryId()!= null)){
            CategoryService.updateCategory(Category);
        }else {
            CategoryService.addCategory(Category);
        }
    }
    @RequestMapping("del")
    public void delCategory(@RequestParam("id") Integer id){
        CategoryService.deleteCategoryById(id);
    }
    @RequestMapping("getAll")
    public PageInfo<Category> getAll(Integer pageNum){
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
        List<Category> CategoryList = CategoryService.findAll();
        PageInfo<Category> CategoryPageInfo = new PageInfo<Category>(CategoryList);
        return CategoryPageInfo;
    }

}
